/*
 * TerrainGrid.cpp
 *
 *  Created on: Jun 30, 2010
 *      Author: Ry
 */

#include <math/Vector3.h>
#include "TerrainGrid.h"

TerrainGrid::TerrainGrid(unsigned int rows, unsigned int cols) {
	this->rows = rows;
	this->cols = cols;
	size = cols * rows;
	squares = new TerrainSquare[size];



//
//	// 4 vertices per square, 3 points per vertex
//	GLfloat gl_vertices[size * 4 * 3];
//	Vector3 vertices[size * 4];
//
//
//	for (unsigned int row = 0; row < rows; row++) {
//		for (unsigned int col = 0; col < cols; col++) {
//
//			// Go ahead an default occupiedBy to NULL for each square
//			unsigned int i = row * width + col;
//			squares[i].occupiedBy = NULL;
//
//			gl_vertices[i * 4 + 0 * 3 + 0] = 1 + col; // X
//			gl_vertices[i * 4 + 0 * 3 + 1] = 0;       // Y
//			gl_vertices[i * 4 + 0 * 3 + 2] = 1 + row; // Z
//
//
//
//			vertices[i * 4 + 1](1 + col, 0, 1 + row);
//			vertices[i * 4 + 2](1 + col, 0, 1 + row);
//			vertices[i * 4 + 3](1 + col, 0, 1 + row);
//		}
//	}
//
//	glGenVertexArrays(1, &g_vao);
//	glBindVertexArray(g_vao);
//	glGenBuffers(1, &g_vertices);
//	glBindBuffer(GL_ARRAY_BUFFER, g_vertices);
//	glBufferData(GL_ARRAY_BUFFER, )








	// Initialize all the squares to NULL
	for (unsigned int i = 0; i < size; i++) {
		squares[i].occupiedBy = NULL;
	}

	list = glGenLists(1);
	glNewList(list, GL_COMPILE);
	glTranslatef(0, -0.02F, 0);
	glBegin(GL_QUADS);
	for (unsigned int row = 0; row < rows; row++) {
		for (unsigned int col = 0; col < cols; col++) {
			glVertex3i(1 + col, 0, 1 + row);
			glVertex3i(1 + col, 0, 0 + row);
			glVertex3i(0 + col, 0, 0 + row);
			glVertex3i(0 + col, 0, 1 + row);
		}
	}
	glEnd();
	glEndList();
}

void TerrainGrid::draw() {
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	glCallList(list);
}

TerrainGrid::~TerrainGrid() {
	delete[] squares;
}
